<?php
defined('IN_IA') || die('Access Denied');
/*
 * CREATE TABLE IF NOT EXISTS `ims_healer_tplmsg` (
  `id` int(10) NOT NULL AUTO_INCREMENT COMMENT '行id',
  `uniacid` int(10) NOT NULL COMMENT '公号id',
  `title` varchar(255) NOT NULL COMMENT '标题',
  `mid` varchar(128) NOT NULL COMMENT '微信模板消息id',
  `content` varchar(500) NOT NULL COMMENT '微信模板内容',
  `data` text NOT NULL COMMENT '模板参数',
  `url` varchar(255) NOT NULL,
  `createtime` int(10) NOT NULL COMMENT '创建时间',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `ims_healer_tplmsg_bulk` (
  `id` int(10) NOT NULL AUTO_INCREMENT COMMENT '行id',
  `uniacid` int(10) NOT NULL COMMENT '统一公号id',
  `mid` int(10) NOT NULL COMMENT '模板消息id',
  `remark` varchar(255) NOT NULL COMMENT '备注',
  `status` tinyint(1) NOT NULL DEFAULT '0' COMMENT '0待群发|1群发中|2群发完成',
  `createtime` int(10) NOT NULL COMMENT '创建时间',
  `group` int(10) NOT NULL DEFAULT '-1',
  `type` tinyint(1) NOT NULL DEFAULT '0',
  `openids` text NOT NULL COMMENT '粉丝编号',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
 * */

if(!pdo_fieldexists('healer_tplmsg', 'times')) {
    pdo_query("ALTER TABLE ".tablename('healer_tplmsg')." ADD `times` int(11) NOT NULL COMMENT '活动推送时间';");
}

if(!pdo_fieldexists('healer_tplmsg', 'status')) {
    pdo_query("ALTER TABLE ".tablename('healer_tplmsg')." ADD `status` int(1) NOT NULL default 0 COMMENT '是否自动推送';");
}
if(!pdo_fieldexists('tg_collect', 'active_before')) {
    pdo_query("ALTER TABLE ".tablename('tg_collect')." ADD `active_before` int(1) NOT NULL default 0 COMMENT '活动前自动推送';");
}
if(!pdo_fieldexists('tg_collect', 'active_after')) {
    pdo_query("ALTER TABLE ".tablename('tg_collect')." ADD `active_after` int(1) NOT NULL default 0 COMMENT '未到自动推送';");
}

global $_W;
global $_GPC;
$op = !empty($_GPC['op']) ? $_GPC['op'] : 'index';
if ($op == "index") {
    $pageindex = max(1, intval($_GPC['page']));
    $pagesize = 20;
    $where = ' WHERE uniacid=:uniacid';
    $params = array(':uniacid' => $_W['uniacid']);
    if (!empty($_GPC['title'])) {
        $where .= ' AND `title` LIKE :title';
        $params[':title'] = '%' . trim($_GPC['title']) . '%';
    }
    if (!empty($_GPC['mid'])) {
        $where .= ' AND `mid`=:mid';
        $params[':mid'] = trim($_GPC['mid']);
    }
    $order = ' ORDER BY `createtime` DESC,`id` DESC';
    $sql = 'SELECT COUNT(*) FROM ' . tablename('healer_tplmsg') . $where;
    $total = pdo_fetchcolumn($sql, $params);
    $pager = pagination($total, $pageindex, $pagesize);
    $sql = 'SELECT * FROM ' . tablename('healer_tplmsg') . $where . $order . ' LIMIT ' . ($pageindex - 1) * $pagesize . ',' . $pagesize;
    $list = pdo_fetchall($sql, $params);
    include wl_template("member/manager");
    exit();
}
if ($op == 'save') {
    if (checksubmit()) {
        if (empty($_GPC['title'])) {
            message('温馨提示：请填写标题！', '', 'error');
        }
        if (empty($_GPC['mid'])) {
            message('温馨提示：请填写模板ID！', '', 'error');
        }
        $value = array();
        foreach ($_GPC['key'] as $key => $v) {
            $value[$v] = array('value' => trim($_GPC['value'][$key]), 'color' => trim($_GPC['color'][$key]));
        }
        $data = array(
            'uniacid' => $_W['uniacid'],
            'title' => trim($_GPC['title']),
            'mid' => trim($_GPC['mid']),
            'content' => trim($_GPC['content']),
            'data' => json_encode($value),
            'url' => trim($_GPC['url']),
            'times' => trim($_GPC['times'])
        );
        if (pdo_insert('healer_tplmsg', $data)) {
            message('温馨提示：新增模板消息成功！', referer(), 'success');
        }
        message('温馨提示：新增模板消息失败！', '', 'error');
    }
    include wl_template("member/manager");
    exit();
}
if ($op == 'update') {
    if (checksubmit()) {
        if (empty($_GPC['title'])) {
            message('温馨提示：请填写标题！', '', 'error');
        }
        if (empty($_GPC['mid'])) {
            message('温馨提示：请填写模板ID！', '', 'error');
        }
        $value = array();
        foreach ($_GPC['key'] as $key => $v) {
            $value[$v] = array('value' => trim($_GPC['value'][$key]), 'color' => trim($_GPC['color'][$key]));
        }
        $data = array(
            'title' => trim($_GPC['title']),
            'mid' => trim($_GPC['mid']),
            'content' => trim($_GPC['content']),
            'data' => json_encode($value),
            'url' => trim($_GPC['url']),
            'times' => trim($_GPC['times'])
        );
        if (pdo_update('healer_tplmsg', $data, array('uniacid' => $_W['uniacid'], 'id' => intval($_GPC['id'])))) {
            message('温馨提示：编辑模板消息成功！', referer(), 'success');
        }
        message('温馨提示：编辑模板消息失败！', '', 'error');
    }
    $tplmsg = pdo_fetch('SELECT * FROM ' . tablename('healer_tplmsg') . ' WHERE `uniacid`=:uniacid AND `id`=:id', array(':uniacid' => $_W['uniacid'], ':id' => intval($_GPC['id'])));
    if (empty($tplmsg)) {
        message('温馨提示：该模板消息不存在！', '', 'error');
    }
    $tplmsg['data'] = json_decode($tplmsg['data'], true);
    include wl_template("member/manager");
    exit();
}
if ($op == 'del') {
    $tplmsg = pdo_fetch('SELECT * FROM ' . tablename('healer_tplmsg') . ' WHERE `uniacid`=:uniacid AND `id`=:id', array(':uniacid' => $_W['uniacid'], ':id' => intval($_GPC['id'])));
    if (empty($tplmsg)) {
        message('温馨提示：该模板消息不存在！', '', 'error');
    }
    if (pdo_delete('healer_tplmsg', array('uniacid' => $_W['uniacid'], 'id' => intval($_GPC['id'])))) {
        message('温馨提示：删除模板消息成功！', referer(), 'success');
    }
    message('温馨提示：删除模板消息失败！', '', 'error');
}
if ($op == 'upsave') {
    $tplmsg = pdo_fetch('SELECT * FROM ' . tablename('healer_tplmsg') . ' WHERE `uniacid`=:uniacid AND `id`=:id', array(':uniacid' => $_W['uniacid'], ':id' => intval($_GPC['id'])));
    $tplmsg['data'] = json_decode($tplmsg['data'], true);
    load()->model('mc');
    $groups = mc_fans_groups(true);
    if (checksubmit()) {
        if (empty($_GPC['remark'])) {
            message('温馨提示：请填写备注！', '', 'error');
        }
        $data = array(
            'uniacid' => $_W['uniacid'],
            'mid' => intval($_GPC['mid']),
            'group' => intval($_GPC['type']) == 0 ? '-1' : trim($_GPC['group']),
            'remark' => trim($_GPC['remark']), 'status' => 0,
            'type' => intval($_GPC['type']),
            'openids' => trim($_GPC['openids']),
            'createtime' => TIMESTAMP
        );
        if (pdo_insert('healer_tplmsg_bulk', $data)) {
            message('温馨提示：保存群发成功！即将进入群发任务……', web_url('member/time', array('op' => 'upsend', 'id' => pdo_insertid())), 'success');
        }
        message('温馨提示：保存群发失败！', '', 'error');
    }
    include wl_template("member/bulk");
    exit();
}
if ($op == 'upsend') {
    $id = intval($_GPC['id']);
    $bulk = pdo_fetch('SELECT * FROM ' . tablename('healer_tplmsg_bulk') . ' WHERE `uniacid`=:uniacid AND `id`=:id', array(':uniacid' => $_W['uniacid'], ':id' => intval($_GPC['id'])));
    if ($bulk['status'] == 0) {
        $account = WeAccount::create($_W['acid']);
        $tplmsg = pdo_fetch('SELECT * FROM ' . tablename('healer_tplmsg') . ' WHERE `uniacid`=:uniacid AND `id`=:id', array(':uniacid' => $_W['uniacid'], ':id' => $bulk['mid']));
        $tplmsg['data'] = json_decode($tplmsg['data'], true);
        $where = 'uniacid=:uniacid AND follow=:follow';
        $params = array(':uniacid' => $_W['uniacid'], ':follow' => 1);
        if ($bulk['type'] == 1) {
            $where .= ' AND `groupid`=:groupid';
            $params[':groupid'] = $bulk['group'];
        } else {
            if ($bulk['type'] == 2) {
                $sql = '';
                $openids = explode("\r\n", $bulk['openids']);
                $i = 0;
                while ($i < count($openids)) {
                    $i == 0 ? $sql .= '"' . $openids[$i] . '"' : ($sql .= ',"' . $openids[$i] . '"');
                    ++$i;
                }
                $where .= ' AND `openid` in (' . $sql . ')';
            }
        }
        $page = max(1, intval($_GPC['page']));
        $pagesize = 100;
        $fansCount = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('mc_mapping_fans') . ' WHERE ' . $where, $params);
        $total = ceil($fansCount / $pagesize);
        $fans = pdo_fetchall('SELECT * FROM ' . tablename('mc_mapping_fans') . ' WHERE ' . $where . ' LIMIT ' . ($page - 1) * $pagesize . ',' . $pagesize, $params);
        foreach ($fans as $key => $fan) {
            $account->sendTplNotice($fan['openid'], $tplmsg['mid'], $tplmsg['data'], $tplmsg['url']);
        }
        if ($page < $total) {
            message('温馨提示：请不要关闭页面，群发任务正在进行中！（' . $page . '/' . $total . '）', web_url('member/time', array('op' => 'upsend', 'id' => $bulk['id'], 'page' => $page + 1)), 'error');
        } else {
            if ($page == $total) {
                if (pdo_update('healer_tplmsg_bulk', array('status' => 2), array('uniacid' => $_W['uniacid'], 'id' => $bulk['id']))) {
                    message('温馨提示：群发任务已完成！（' . $page . '/' . $total . '）', web_url('member/time', array('op' => 'index')), 'success');
                }
                message('温馨提示：群发任务出错！', '', 'error');
            } else {
                pdo_update('healer_tplmsg_bulk', array('status' => 2), array('uniacid' => $_W['uniacid'], 'id' => $bulk['id']));
                message('温馨提示：该群发任务已完成！', web_url('member/time'), 'error');
            }
        }
    } else {
        if ($bulk['status'] == 1) {
            message('温馨提示：该群发任务正在进行中！', '', 'error');
        } else {
            if ($bulk['status'] == 2) {
                message('温馨提示：该群发任务已完成！', '', 'error');
            }
        }
    }
}
if ($op == 'updel') {
    $bulk = pdo_fetch('SELECT * FROM ' . tablename('healer_tplmsg_bulk') . ' WHERE `uniacid`=:uniacid AND `id`=:id', array(':uniacid' => $_W['uniacid'], ':id' => intval($_GPC['id'])));
    if (empty($bulk)) {
        message('温馨提示：该群发任务不存在！', '', 'error');
    }
    if (pdo_delete('healer_tplmsg_bulk', array('uniacid' => $_W['uniacid'], 'id' => intval($_GPC['id'])))) {
        message('温馨提示：删除群发任务成功！', referer(), 'success');
    }
    message('温馨提示：删除群发任务失败！', '', 'error');
}
if ($op == "upindex") {
    $pageindex = max(1, intval($_GPC['page']));
    $pagesize = 20;
    $where = ' WHERE uniacid=:uniacid';
    $params = array(':uniacid' => $_W['uniacid']);
    if (!empty($_GPC['remark'])) {
        $where .= ' AND `remark` LIKE :remark';
        $params[':remark'] = '%' . trim($_GPC['remark']) . '%';
    }
    if (is_numeric($_GPC['status'])) {
        $where .= ' AND `status`=:status';
        $params[':status'] = trim($_GPC['status']);
    }
    $order = ' ORDER BY `createtime` DESC,`id` DESC';
    $sql = 'SELECT COUNT(*) FROM ' . tablename('healer_tplmsg_bulk') . $where;
    $total = pdo_fetchcolumn($sql, $params);
    $pager = pagination($total, $pageindex, $pagesize);
    $sql = 'SELECT * FROM ' . tablename('healer_tplmsg_bulk') . $where . $order . ' LIMIT ' . ($pageindex - 1) * $pagesize . ',' . $pagesize;
    $list = pdo_fetchall($sql, $params);
    include wl_template("member/bulk");
}
if ($op == 'status') {
    $id = intval($_GPC['id']);
    $status = intval($_GPC['status']);
    pdo_update('healer_tplmsg', array('status' => $status), array('id' => $id));
    die(json_encode(array('errno' => 0, 'message' => "设置成功")));

}